package com.mall.b2c.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.mall.b2c.bean.ConsultBean;
import com.mall.b2c.dao.ConsultDao;
import com.mall.b2c.domain.goods.GoodsDomain;
import com.mall.b2c.domain.member.ConsultDomain;
import com.mall.b2c.domain.member.MemberDomain;
import com.mall.b2c.enums.ConsultType;
import com.mall.b2c.service.ConsultService;
import com.mall.b2c.service.GoodsService;
import com.mall.b2c.service.MemberService;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.impl.BaseServiceImpl;


/**
 * Service实现类 - 咨询、建议、投诉、纠错
 * @author lzx
 */
@Service("b2cConsultService")
@Transactional(readOnly=true)
public class ConsultServiceImpl extends BaseServiceImpl<ConsultDomain, Long> implements ConsultService {
	
	@Resource private MemberService memberService;
	@Resource private GoodsService goodsService;
	
	@Resource
	public void setBaseDao(ConsultDao consultDao) { 
		super.setBaseDao(consultDao); 
	}

	@Override
	@Transactional( propagation=Propagation.REQUIRED )
	public ConsultDomain saveConsult(ConsultBean bean, Long memberId) {
		
		MemberDomain member = memberService.find(memberId);
		if(member == null){
			throw new NoticeException("会员不存在");
		}
		
		ConsultDomain consult = new ConsultDomain();
		consult.setTitle(bean.getTitle());
		consult.setType(ConsultType.valueOf(bean.getType()));
		consult.setAttachFile(bean.getAttachFile());
		consult.setContact(bean.getContact());
		consult.setContent(StringEscapeUtils.escapeHtml4(bean.getContent()));
		consult.setMember(member);
		consult.setReadStatus(false);
		consult.setReturnStatus(false);
		consult.setDeleteStatus(false);
		this.persist(consult);

		return consult;
	}

	@Override
	@Transactional( propagation=Propagation.REQUIRED )
	public void saveCorrect(Long goodsId, String content, Long memberId) {
		GoodsDomain goods = goodsService.find(goodsId);
		if(goods == null){
			throw new NoticeException("商品不存在");
		}
		MemberDomain member = memberService.find(memberId);
		if(member == null){
			throw new NoticeException("会员帐号不存在");
		}
		
		//限制提交数量
		checkValid(memberId);
		
		ConsultDomain correct = new ConsultDomain();
		correct.setTitle("商品纠错["+goods.getCode()+"]");
		correct.setContent(StringEscapeUtils.escapeHtml4(content));
		correct.setGoods(goods);
		correct.setContact(member.getMobile());
		correct.setMember(member);
		correct.setType(ConsultType.correct);
		correct.setDeleteStatus(false);
		correct.setReadStatus(false);
		correct.setReturnStatus(false);
		this.persist(correct);
		
	}

	@Override
	@Transactional( propagation=Propagation.REQUIRED )
	public void deleteConsult(Long[] consultIds, Long memberId) {
		String hql = "from ConsultDomain bean where bean.id in (:ids) and bean.member.id = :memberId";
		Finder finder = Finder.create(hql);
		finder.setParamList("ids", consultIds);
		finder.setParam("memberId", memberId);
		List<ConsultDomain> consultList = finder.findHQLList();
		
		if(!CollectionUtils.isEmpty(consultList)){
			for(ConsultDomain consult : consultList){
				consult.setDeleteStatus(true);
				this.update(consult);
			}
		}else{
			throw new NoticeException("咨询消息不存在");
		}
		
	}
	
	/**
	 * 限制提交消息数量，如果超出100条未读且未删除消息则抛出异常提示信息
	 * @param memberId
	 */
	private void checkValid(Long memberId){
		String hql = "select count(bean) from ConsultDomain bean where bean.member.id = :memberId and bean.readStatus = false and bean.deleteStatis = false";
		Finder finder = Finder.create(hql);
		finder.setParam("memberId", memberId);
		Object obj = finder.findUniqueByHQL();
		if(obj != null && (Long) obj > 100L){
			throw new NoticeException("抱歉，你提交的信息数量超出限制，请稍后再试");
		}
	}

}
